package com.wh.productMag.controller;

import com.wh.basic.vo.AjaxResult;
import com.wh.productMag.service.IProductListService;
import com.wh.productMag.pojo.ProductList;
import com.wh.productMag.dto.ProductListQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/productList")
public class ProductListController {

    @Autowired
    public IProductListService productListService;

    /**
     * 接口：添加或修改
     * @param productList 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductList productList) {
        if ( productList.getId() != null){
            productListService.updateProduct(productList);
        }else{
            productListService.save(productList);
        }
        return new AjaxResult();
    }
    @PutMapping("/addData")
    public AjaxResult addData(@RequestBody ProductList productList){
        productListService.addData(productList);
        return new AjaxResult();
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        productListService.removeById(id);
        return new AjaxResult();
    }
    @DeleteMapping(value = "/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Integer id) {
        productListService.deletebyid(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        productListService.removeByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public ProductList findOne(@PathVariable("id") Long id) {
        return productListService.getById(id);
    }
    /**
     * 查询产品数量
     */
    @GetMapping("/num")
    public Integer allNum(){
        return productListService.allNum();
    }
    //根据订单查询
    @GetMapping("/list/{id}")
    public List<ProductList> findproduct(@PathVariable("id") Long id) {
        return  productListService.getproduct(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<ProductList> findAll() {
        return productListService.list();
    }


    /**
     * 接口：分页查询或高级查询
     * @param productListQueryDto 查询对象
     * @return IPage<ProductList> 分页对象
     */
    @PostMapping
    public IPage<ProductList> findByPage(@RequestBody ProductListQueryDto productListQueryDto) {
        return productListService.findByPage(productListQueryDto);

    }@PostMapping("/forSale")
    public IPage<ProductList> forSale(@RequestBody ProductListQueryDto productListQueryDto) {
        return productListService.forSale(productListQueryDto);

    }
    //产品下架
    @DeleteMapping  ("/offShelf/{id}")
    public AjaxResult offShelf (@PathVariable("id") Integer id){
        productListService.offShelf(id);
        return new AjaxResult();
    }
}
